/*
 邀请面试对话框
 @绑定查看联系方式
 */
$(function(){
    $.extend({
        //企业将个人纳入人才库
        insertPersonnelLibrary:function(settings){
            var setvar={
                className:"",
                crsf:"",
                url:"",
                personnelLibraryUrl:"",
                delPersonnelLibrary:"",
                postJobUrl:"",
                resumeId:"",
                userName:"",
                callBack:function(){}
            };
            $.extend(setvar,settings);

            toInterView = layer.open({
                type: 1,
                title:"<b style='color:#ffae01'>请您给[<i style='color: #666;'>"+setvar.userName+"</i>]分配一个合适的岗位</b>",
                skin: 'layer-insertPersonnelLibrary',
                area: ['840px', '638px'],
                resize:false,
                move:false,
                btn:false,
                content:"<div id='toInterViewDiv'></div>",
                success:function(){
                    $.post(setvar.url,{
                        '_token': setvar.crsf,
                        'resumeId': setvar.resumeId
                    },function (data) {
                        switch (data.status){
                            case 2:
                                //layer.msg('亲，您还未发布工作岗位！', {icon: 6});
                                setvar.callBack(data.status);
                                break;
                            case 1:
                                jobTitle = data.jobTitle;
                                html = '';
                                //option
                                html += "<div class='insertPersonnelLibrary'>";
                                html += "<form action='#' method='post' name='insertPersonnelLibraryForm' id='insertPersonnelLibraryForm' >";//company/interviewnote
                                html += "<input type='hidden' name='_token' value='"+setvar.crsf+"'>";
                                html += "<input type='hidden' name='resumeId' value='"+setvar.resumeId+"'>";

                                html += "<ul class='checkList'>";
                                html += "<li class='first titleButtom bloder'>序号</li>";
                                html += "<li class='second titleButtom bloder'>岗位名称</li>";
                                html += "<li class='third titleButtom bloder'>职位薪资</li>";
                                html += "<li class='four titleButtom bloder'>工作地区</li>";
                                html += "<li class='five titleButtom bloder'>更新时间</li>";
                                html += "<li class='six titleButtom bloder'>操作</li>";
                                html += "</ul>";
                                //显示已纳入人才
                                html += "<div id='havePersonnelShow'></div>";
                                haveHtml = "";

                                if(jobTitle.length == 0){
                                    html += "<div class='haveNoJob'>";
                                    html += "<p class='first-tip'>您还没有发布的职位</p>";
                                    html += "<p class='second-tip'>发布职位，320万人才给您投递简历</p>";
                                    html += "<a href="+setvar.postJobUrl+" class='button' target='_blank'>立即发布职位</a>";
                                    html += "</div>";
                                }

                                $.each( jobTitle, function(key, value){
                                    //已纳入人才库
                                    if(value.personnel_is_have_insert != null){
                                        if(value.personnel_is_have_insert.have == 1){
                                            haveHtml += "<ul class='list HaveColor' id='list"+(key+1)+"'>";
                                            haveHtml += "<li class='first'>"+(key+1)+"</li>";
                                            haveHtml += "<li class='second'>"+value.job_title+"</li>";
                                            haveHtml += "<li class='third'>"+value.salary_above+"</li>";

                                            haveHtml += "<li class='four'>"+value.location+"</li>";
                                            haveHtml += "<li class='five'>"+value.updatetime+"</li>";
                                            haveHtml += "<li class='six'><span class='delete'><em>取消分配</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
                                            haveHtml += "</ul>";
                                        }else{
                                            html += "<ul class='list dropHaveColor' id='list"+(key+1)+"'>";
                                            html += "<li class='first'>"+(key+1)+"</li>";
                                            html += "<li class='second'>"+value.job_title+"</li>";
                                            html += "<li class='third'>"+ value.salary_above+"</li>";

                                            html += "<li class='four'>"+value.location+"</li>";
                                            html += "<li class='five'>"+value.updatetime+"</li>";
                                            html += "<li class='six'><span class='add'><em>分配该岗位</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
                                            html += "</ul>";
                                        }
                                    }else{
                                        html += "<ul class='list dropHaveColor' id='list"+(key+1)+"'>";
                                        html += "<li class='first'>"+(key+1)+"</li>";
                                        html += "<li class='second'>"+value.job_title+"</li>";
                                        html += "<li class='third'>"+value.salary_above+"</li>";

                                        html += "<li class='four'>"+value.location+"</li>";
                                        html += "<li class='five'>"+value.updatetime+"</li>";
                                        html += "<li class='six'><span class='add'><em>分配该岗位</em><input type='hidden' value='"+value.id+"'><input type='hidden' value='"+(key+1)+"'></span></li>";
                                        html += "</ul>";
                                    }
                                });
                                html += "</form>";
                                html += "</div>";
                                //确定按钮
                                html += "<div class='confirm'>";
                                html += "<input type='button' value='确 定'>";
                                html += "</div>";
                                $('#toInterViewDiv').html(html);

                                //追加到显示纳入人才div
                                $("#havePersonnelShow").html(haveHtml);
                                //确定关闭按钮
                                $(".confirm").find("input").eq(0).click(function(){
                                    layer.close(toInterView);
                                });
                                $(".list").find(".six").each(function(){
                                    $(this).find(".add").eq(0).click(function(){
                                        job_id = $(this).find("input").eq(0).val();
                                        thisId = $(this);
                                        $.post(setvar.personnelLibraryUrl+"?resume_id="+setvar.resumeId+"&job_id="+job_id, {
                                            '_token': setvar.crsf
                                        },function (data) {
                                            if (data.status == 0) {
                                                switch (thisId.attr("class")){
                                                    case "add":
                                                        $(".list").find(".six").find(".delete").find("em").html("分配该岗位");
                                                        $(".list").find(".six").find(".delete").attr("class","add");
                                                        thisUiId = thisId.find("input").eq(1).val();
                                                        thisId.attr("class",'delete');
                                                        thisId.find("em").html("取消分配");
                                                        $(".list").attr("class","list dropHaveColor")
                                                        $("#list"+thisUiId).attr("class","list HaveColor");
                                                        layer.msg(data.msg, {icon: 6});
                                                        break;
                                                    case "delete":
                                                        $.post(setvar.delPersonnelLibrary+"?resume_id="+setvar.resumeId+"&job_id="+job_id, {
                                                            '_token': setvar.crsf
                                                        },function (data) {
                                                            if (data.status == 0) {
                                                                thisId.attr("class",'add');
                                                                thisId.find("em").html("分配该岗位");
                                                                thisUiId = thisId.find("input").eq(1).val();
                                                                $(".list").attr("class","list dropHaveColor");
                                                                layer.msg(data.msg, {icon: 6});
                                                                return true;
                                                            } else {
                                                                layer.msg(data.msg, {icon: 5});
                                                                return false;
                                                            }
                                                        });
                                                        break;
                                                }
                                                return true;
                                            } else {
                                                layer.msg(data.msg, {icon: 5});
                                                return false;
                                            }
                                        });
                                    });
                                });

                                //取消分配
                                $(".list").find(".six").each(function(){
                                    $(this).find(".delete").eq(0).click(function(){
                                        job_id = $(this).find("input").eq(0).val();
                                        thisId = $(this);
                                        $.post(setvar.delPersonnelLibrary+"?resume_id="+setvar.resumeId+"&job_id="+job_id, {
                                            '_token': setvar.crsf
                                        },function (data) {
                                            if (data.status == 0) {
                                                switch (thisId.attr("class")){
                                                    case "delete":
                                                        $(".list").find(".six").find(".delete").find("em").html("分配该岗位");
                                                        $(".list").find(".six").find(".delete").attr("class","add");
                                                        thisUiId = thisId.find("input").eq(1).val();
                                                        $(".list").attr("class","list dropHaveColor")
                                                        layer.msg(data.msg, {icon: 6});
                                                        break;
                                                    case "add":
                                                        $.post(setvar.personnelLibraryUrl+"?resume_id="+setvar.resumeId+"&job_id="+job_id, {
                                                            '_token': setvar.crsf
                                                        },function (data) {
                                                            if (data.status == 0) {
                                                                $(".list").find(".six").find(".delete").find("em").html("分配该岗位");
                                                                $(".list").find(".six").find(".delete").attr("class","add");
                                                                thisUiId = thisId.find("input").eq(1).val();
                                                                thisId.attr("class",'delete');
                                                                thisId.find("em").html("取消分配");
                                                                $(".list").attr("class","list dropHaveColor")
                                                                $("#list"+thisUiId).attr("class","list HaveColor");
                                                                layer.msg(data.msg, {icon: 6});
                                                                return true;
                                                            } else {
                                                                layer.msg(data.msg, {icon: 5});
                                                                return false;
                                                            }
                                                        });
                                                        break;
                                                }
                                                return true;
                                            } else {
                                                layer.msg(data.msg, {icon: 5});
                                                return false;
                                            }
                                        });
                                    });
                                });
                            break;
                        }
                    });
                }
            });
        }
    });
});
